﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace test2
{
    public abstract class Shape
    {
        public abstract double Area();
    }
    public class Rectangle : Shape //矩形
    {
        public double len { get; set; }
        public double wid { get; set; }

        public Rectangle() { }
        public Rectangle(double len, double wid)
        {
            if (len <= 0 || wid <= 0)
                throw new Exception(len + "," + wid + "无法构成矩形！");
            this.len = len;
            this.wid = wid;
        }

        public bool IsCorrect() 
        {
            if (len <= 0 || wid <= 0)
                return false;
            else 
                return true; 
        }

        public override double Area() 
        {
            if (IsCorrect())
                return len * wid;
            else
                return -1;
        }

    }

    class Triangle : Shape //三角形 
    {
        public double a { get; set; }
        public double b { get; set; }
        public double c { get; set; }
        public Triangle() { }
        public Triangle(double a, double b, double c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public bool IsCorrect()
        {
            if (a <= 0 || b <= 0 || c <= 0 || a + b <= c || a + c <= b || b + c <= a)
                return false;
            else
                return true;
        }

        public override double Area()
        {
            double p = (a + b + c) / 2;
            return Math.Round(Math.Sqrt((p * (p - a) * (p - b) * (p - c))), 4);
        }
    }

    class Square : Shape   //正方形
    {
        public double edge {  get; set; }

        public Square() { }
        public Square(double edge)
        {
            this.edge = edge;            
        }

        public bool IsCorrect()
        {
            if (edge <= 0)
                return false;
            else
                return true;
        }

        public override double Area()
        {
            return edge * edge;
        }
    }

    class Circle : Shape   //圆形
    {
        public double rad {  get; set; }
        public Circle() { }
        public Circle(double rad)
        {
            this.rad = rad;        
        }

        public bool IsCorrect()
        {
            if (rad <= 0)
                return false;
            else
                return true;
        }

        public override double Area()
        {
            return 3.14 * rad * rad;
        }
    }

    public class SimpleFactory
    {
        public static Shape CreatShape(string shape)
        {
            Shape newShape = null;
            if (shape == "Triangle")
            {
                Random rand = new Random();
                bool flag = false;
                while (!flag)
                {
                    double a = rand.Next(1, 10);
                    double b = rand.Next(1, 10);
                    double c = rand.Next(1, 10);
                    if (!(a + b <= c || a + c <= b || b + c <= a))
                    {
                        flag = true;
                        Console.WriteLine("三角形  a={0}, b={1}, c={2}", a, b, c);
                        newShape = new Triangle(a, b, c);
                    }
                }
            }
            else if (shape == "Rectangle")
            {
                Random rand = new Random();

                double a = rand.Next(1, 10);
                double b = rand.Next(1, 10);
                Console.WriteLine("矩  形  a={0}, b={1}", a, b);
                newShape = new Rectangle(a, b);
            }
            else if (shape == "Square")
            {
                Random rand = new Random();

                double a = rand.Next(1, 10);
                Console.WriteLine("正方形  a={0}", a);
                newShape = new Square(a);
            }
            else if (shape == "Circle")
            {
                Random rand = new Random();

                double r = rand.Next(1, 10);
                Console.WriteLine("圆  形  r={0}", r);
                newShape = new Circle(r);
            }
            else
                newShape = new Square(0);
            return newShape;
        }
        static void Main(string[] args)
        {
            int N = 1;
            double sum = 0;
            string[] shape = { "Triangle", "Rectangle", "Square", "Circle" };
            Random random = new Random();
            while (N <= 10)
            {
                Console.Write("第{0}个形状：", N);
                int i = random.Next(0, 4);
                Shape s = SimpleFactory.CreatShape(shape[i]);
                Console.WriteLine("面积：" + s.Area());
                sum += s.Area();
                N++;
            }
            Console.WriteLine("\n总面积：" + Math.Round(sum, 4));
            Console.ReadKey();
        }
    }
    public class ClientSimple
    {
        void Start()
        {
            //简单工厂
            Shape Shape1 = SimpleFactory.CreatShape("Triangle");
            Shape1.Area();
            Shape Shape2 = SimpleFactory.CreatShape("Square");
            Shape2.Area();
            Shape Shape3 = SimpleFactory.CreatShape("Circle");
            Shape3.Area(); 
            Shape Shape4 = SimpleFactory.CreatShape("Rectangle");
            Shape4.Area();
        }
    }
}
